package mephisto.services.impl;

import java.io.IOException;
import java.io.InputStream;

import mephisto.Context;
import mephisto.model.Content;
import mephisto.services.IContentService;

public class ContentService extends BaseService implements IContentService {

    public ContentService() {
        super();

    }

    public Content getContent(String name, String namespace, Context context) {
        InputStream is = mephisto.getStorageService().get(name, context);
        String content;
        try {
            content = Utils.streamToString(is);
            Content p = new Content(name, namespace);
            p.setContent(content);
            return p;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }

    public String getLaidOutRenderedContent(String pageId, Context context) {
        // if (!(main.equals("css") || main.equals("mephisto"))) {
        StringBuilder output = new StringBuilder();
        output.append("<html><header>");
        output.append(getRenderedPage("header", context));
        output.append("</header><body>");
        output.append(getRenderedPage("top", context));
        output.append("<table class=\"main-table\" width=\"100%\" cellpadding='5' cellspacing='5'>");
        output.append("<tr valign='top'><td>");
        output.append(getRenderedPage("left", context));
        output.append("</td><td width='85%'>");
        output.append(getRenderedPage(pageId, context));
        output.append("</td>");
        output.append("</tr>");
        output.append("<tr valign='top'><td colspan='2'>");
        output.append(getRenderedPage("bottom", context));
        output.append("</td></tr>");
        output.append("</table>");
        output.append("</body></html>");
        return output.toString();
        
    }
    
    public String getRenderedPage(String pageId, Context context) {
        try {
            Content page = getContent(pageId, context);
            String content = page.getContent();
            content = mephisto.getGroovyService().evaluate(content, context);
            content = mephisto.getWikiModelService().render(content, context);
            return content;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
    
    public Content getContent(String pageId, Context context) {
        
        InputStream is = getContentStream(pageId, context);
        String content;
        try {
            content = Utils.streamToString(is);
            Content page = new Content(pageId);
            page.setContent(content);
            return page;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
        
    }
    
    public InputStream getContentStream(String contentId, Context context) {
        return mephisto.getStorageService().get(contentId, context);
    }


    public void saveContent(Content page, Context context) {
        // String uuid = mephisto.getIdService().getId(page.getName(),
        // page.getNamespace());
        mephisto.getStorageService().store(page.getContent(), page.getName(), context);
        // mephisto.getRdfService().
    }

}